#!/bin/sh -e

for patch in *.patch; do
    patch -p1 < "$patch"
done

# Remove dbus dependency.
{
    sed -i 's/use_dbus.*/use_dbus=false/' \
        src/3rdparty/chromium/build/config/features.gni
}

# Remove udev dependency.
{
sed -i '/use_udev/s/=.*/=false/' \
    src/3rdparty/chromium/build/config/features.gni

sed -i 's/\(use_udev=\)true/\1false/' \
    src/buildtools/config/linux.pri
}

# Fix nasm hardcoded glibcism
sed -i '/CANONICALIZE_FILE_NAME/d' \
    src/3rdparty/chromium/third_party/nasm/config/config-linux.h

# Remove glibc header.
sed -i '/execinfo.h/d' \
    src/3rdparty/chromium/base/debug/stack_trace_posix.cc


# The build fails if qtwebengine is already installed.
find . -name '*.pr[fio]' | while read -r file; do
    sed -i "s#INCLUDEPATH += #&\$\$QTWEBENGINE_ROOT/include #" "$file"
done

qmake QMAKE_CXXFLAGS=-DQT_NO_ACCESSIBILITY -- \
    -feature-webengine-system-ninja \
    -feature-webengine-system-zlib \
    -feature-webengine-system-harfbuzz \
    -feature-webengine-system-png \
    -feature-webengine-system-libevent \
    -feature-webengine-system-libvpx \
    -feature-webengine-system-opus \
    -feature-webengine-system-libwebp \
    -feature-webengine-system-ffmpeg \
    -feature-webengine-proprietary-codecs \
    -no-feature-webengine-system-icu \
    -no-feature-webengine-system-glib

make
make INSTALL_ROOT="$1" install
